const db = wx.cloud.database();
// pages/order/order.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tabs: [{
			id: 0,
			value: "全部订单",
			isActive: true
		}, {
			id: 1,
			value: "待付款",
			isActive: false
		}, {
			id: 2,
			value: "待收货",
			isActive: false
		}, {
			id: 3,
			value: "退款/退货",
			isActive: false
		}],
		order_List: [],
		//openid:''
	},

	onShow(options) {
		//const openid = wx.getStorageSync("openid");
		let pages = getCurrentPages();
		let currentPages = pages[pages.length - 1];
	/* 	this.setData({
			openid
		}) */
		
		const {type} = currentPages.options;
		this.changeTitleActive(type-1);		
		this.getAllorder(type);
	},




	TabsItemsC(e) {
		const {
			index
		} = e.detail;
		this.changeTitleActive(index);
		this.getAllorder(index+1);

	},

	async getAllorder(type) {
		const userInfo = wx.getStorageSync('userInfo');
		if (!userInfo) {
			wx.navigateBack({
				delta: 1,
				success: function() {
					wx.showToast({
						title: '请先登录',
						icon: 'none',
						mask: true
					})
				}
			})
			return;
		} else {
			const res = await db.collection('order_List').where({
					'type': Number(type)
				}).get()
				.then((res) => {
					this.setData({
						order_List: res.data
					})
				console.log(res.data)
				})
		}
	},
	

	changeTitleActive(index){
		let {
			tabs
		} = this.data;
		tabs.forEach((v, i) => {
			i === index ? v.isActive = true : v.isActive = false
		});
		this.setData({
			tabs
		})
	}

})
